from framework.response import response
import time

from .tdengine import td_engine
from .base import Util


class CatMiddleware:
    """实现接口监控"""

    @staticmethod
    def get_sql(request, t0: float, t1: float):
        url = request.path
        method = request.method
        ts = Util.ts_to_scale(time.time())
        t = round(t1 - t0, 3)
        sql = f"insert into power.log_2024(ts,url,time,method) values({ts},'{url}',{t},'{method}')"



    def __init__(self, get_response):
        self.get_response = get_response

    def __call__(self, request, *args, **kwargs):
        t0 = time.perf_counter()
        ret = self.get_response(request)
        t1 = time.perf_counter()
        if hasattr(ret, 'exception'):
            exc = ret.content
        else:
            exc = ''
        self.get_sql(request, t0, t1)
        return ret